﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using E2_WEBSITE.Code.Admin;
using E2_WEBSITE.Code;

namespace E2_WEBSITE.cp.admin.site
{
    public partial class adsmanagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDeleteAds_Click(object sender, EventArgs e)
        {
            if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
            {
                WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
                return;
            }
            try
            {
                if (gridAds.Rows.Count > 0)
                {
                    AdsController controller = new AdsController();
                    for (int i = 0; i < gridAds.Rows.Count; i++)
                    {
                        CheckBox cb = gridAds.Rows[i].FindControl("AdsSelector") as CheckBox;
                        if (cb != null && cb.Checked)
                        {
                            int aid = Convert.ToInt32(gridAds.DataKeys[i].Value);
                            controller.DeleteAds(aid);
                        }
                    }
                    Page.Response.Redirect("~/cp/admin/site/adsmanagement.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect(Utilities.ErrorPagePath);
                throw;
            }
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            ToggleAdsSelector(true);
        }

        protected void btnDeselectAll_Click(object sender, EventArgs e)
        {
            ToggleAdsSelector(false);
        }

        private void ToggleAdsSelector(bool checkState)
        {
            if (gridAds.Rows.Count > 0)
            {
                for (int i = 0; i < gridAds.Rows.Count; i++)
                {
                    CheckBox cb = gridAds.Rows[i].FindControl("AdsSelector") as CheckBox;
                    if (cb != null)
                    {
                        cb.Checked = checkState;
                    }
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["Authentication"] == null || Convert.ToInt32(Session["Authentication"]) != 0)
            {
                WebMsgBox.Show("Bạn phải là admin để có thể sử dụng chức năng này.");
                return;
            }
            Page.Response.Redirect("~/cp/admin/site/addads.aspx");
        }
    }
}
